package cn.com.homeyoda.aimate.aihealth.ui.videoinspect;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.ViewModelProviders;

import java.io.File;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.UploadVideoBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityTijianResultBinding;
import cn.com.homeyoda.aimate.aihealth.utils.LoadingStateManager;
import cn.com.homeyoda.aimate.aihealth.utils.ShowLoadingUtil;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import static com.wzq.mvvmsmart.utils.Utils.getContext;

//进度条体检
public class TiJianResultActivity extends BaseActivity<ActivityTijianResultBinding, VideoInspectModel>{

    String reportId;//检测报告id
    String faceId;//
    String deviceId = Build.SERIAL;
    boolean isTiao = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String filePath = getIntent().getStringExtra("file_path");
        deviceId = getIntent().getStringExtra("deviceId");
        Log.i("huage","deviceId==="+deviceId);
        File file = new File(filePath);
        uploadVideo(file);
    }

    @Override
    public VideoInspectModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(VideoInspectModel.class);
    }

    @Override
    public void initData() {
        super.initData();

        initView();
    }

    private void initView() {
        if(isLogin()){
            binding.tip.setVisibility(View.GONE);
        }else{
            binding.tip.setVisibility(View.VISIBLE);
        }

        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });



        ValueAnimator animator = ValueAnimator.ofInt(0, 100);
        animator.setDuration(15000); // 5秒
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int progress = (int) animation.getAnimatedValue();
                binding.progressbar.setProgress(progress);
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                onProgressComplete();
            }
        });
        animator.start();

    }



    private void onProgressComplete(){
        Log.i("haha","onProgressComplete=====");
//        startActivity(new Intent(getContext(), TiJianResultOneActivity.class));
//        finish();
    }


    private void uploadVideo(File videoFile){
        // 2. 创建视频文件的RequestBody
        RequestBody videoRequestBody = RequestBody.create(
                MediaType.parse("video/*"),
                videoFile);
        MultipartBody.Part videoPart = MultipartBody.Part.createFormData(
                "video",
                videoFile.getName(),
                videoRequestBody
        );
        RequestBody deviceIdBody = RequestBody.create(
                MediaType.parse("text/plain"), deviceId
        );
        viewModel.uploadVideo(videoPart,deviceIdBody);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @Override
    public void initViewObservable() {
        viewModel.stateLiveData.observe(this,uploadVideoBean -> {
            if(!uploadVideoBean.getFaceId().equals("")){
                Log.i("huazai","uploadVideoBeans====="+uploadVideoBean.toString());
                reportId = uploadVideoBean.getReportId();
                faceId = uploadVideoBean.getFaceId();
//                viewModel.isShowCompleteInfo(uploadVideoBean.getFaceId());
                viewModel.isShowCompleteInfo(faceId);
                binding.progressbar.setProgress(100);
            }
        });

        viewModel.stateIsShowData.observe(this,isShowCompleteInfoBean -> {
//            Intent intent = new Intent(getContext(), TiJianResultOneActivity.class);
//            intent.putExtra("reportId", reportId);
//            intent.putExtra("faceId", faceId);
//            startActivity(intent);
//            finish();
            Log.i("huazai","stateIsShowData===");
//            if(!isTiao){
//                isTiao = true;
                if(isShowCompleteInfoBean.getData().getIsShowCompleteInfo().equals("false")){
                    Intent intent = new Intent(getContext(), DetectionResultActivity.class);
                    intent.putExtra("reportId", reportId);
                    intent.putExtra("faceId", faceId);
                    startActivity(intent);
                    finish();
                }else{//显示完善信息
                    Intent intent = new Intent(getContext(), TiJianResultOneActivity.class);
                    intent.putExtra("reportId", reportId);
                    intent.putExtra("faceId", faceId);
                    startActivity(intent);
                    finish();
                }

//            }

        });



    }



    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_tijian_result;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
}
